# -*- coding: utf-8 -*-
# @Author : zhaochen
# @Date   : 2019/7/9
# @File   : IC01_Past1MonQueryCnt.py
# @E-mail : zhaochen@bbdservice.com


'''eof
name:最近1月内的查询次数
code:IC01_Past1MonQueryCnt
tableName:
columnName:
groups:个人征信模块
dependencies:IC_DSJ
type:常用指标
datasourceType:在线指标
description:
eof'''


import sys
import re
import datetime
reload(sys)
sys.setdefaultencoding('utf-8')

null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']

def getTable(table_Name):
    try:
        data = IC_DSJ['data'].get(table_Name)
        if data in null_type_list:
            return None
        else:
            # 存在1条为空的数据[{}]
            if isinstance(data, list) and len(data) == 1 and data[0]['reportNo'] in null_type_list:
                return [{}]
            # 存在表 但为空list []
            elif isinstance(data, list) and len(data) == 0:
                return None
            else:
                return data
    except:
        return None

def isfloat(x):
    '''
    判断x为float类型且大于等于0
    :param x:
    :return:
    '''
    try:
        if isinstance(float(x), float) and float(x)>=0:
            if str(x) in ['inf', 'infinity', 'INF', 'INFINITY', 'True']:
                return False
            else:
                return True
        else:
            return False
    except:
        return False



def IC01_Past1MonQueryCnt():
    try:
        ICRRecordSummary002 = getTable('ICRRecordSummary002')

        if ICRRecordSummary002 is None:
            return u'缺失值'
        elif ICRRecordSummary002 == [{}]:
            return 0
        else:
            recordSum1 = ICRRecordSummary002[0].get("recordSum1")
            recordSum2 = ICRRecordSummary002[0].get("recordSum2")

            if isfloat(recordSum1):
                recordSum1 = float(recordSum1)
            else:
                recordSum1 = None

            if isfloat(recordSum2):
                recordSum2 = float(recordSum2)
            else:
                recordSum2 = None

            if recordSum1 is None and recordSum2 is None:
                return u'缺失值'
            else:
                recordSum1 = recordSum1 if recordSum1 != None else 0
                recordSum2 = recordSum2 if recordSum2 != None else 0

                return recordSum1 + recordSum2
    except:
        return u'缺失值'


result = IC01_Past1MonQueryCnt()